package bbk.nfc.opt;

import java.lang.reflect.Method;

import bbk.nfc.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.bluetooth.BluetoothAdapter;
import android.content.ContentResolver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.LocationManager;
import android.media.AudioManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.provider.Settings;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

public class OptSetting
{
	
	public static void toggleWifi(byte opt, Context context)
	{
		//wifi
		WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
		switch (opt)
		{
		case Vars.ENABLE:
			wifiManager.setWifiEnabled(true);
			break;
		case Vars.DISABLE:
			wifiManager.setWifiEnabled(false);
			break;
		case Vars.TOGGLE:
			if (wifiManager.isWifiEnabled())
			{
				wifiManager.setWifiEnabled(false);
			}
			else
			{
				wifiManager.setWifiEnabled(true);
			}
			break;
		}
	}
	
	//bt
	public static void toggleBT(byte opt)
	{
		BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
		switch (opt)
		{
		case Vars.ENABLE:
			adapter.enable();
			break;
		case Vars.DISABLE:
			adapter.disable();
			break;
		case Vars.TOGGLE:
			int state = adapter.getState();
			if (state == BluetoothAdapter.STATE_OFF)
				adapter.enable();
			else if (state == BluetoothAdapter.STATE_ON)
				adapter.disable();
			break;
		}
	}
	
	//gps
	public static void toggleGPS(byte opt, Context context)
	{
		try
		{
			ContentResolver cr = context.getContentResolver();
			switch (opt)
			{
			case Vars.ENABLE:
				Settings.Secure.setLocationProviderEnabled(cr, LocationManager.GPS_PROVIDER, true);
				break;
			case Vars.DISABLE:
				Settings.Secure.setLocationProviderEnabled(cr, LocationManager.GPS_PROVIDER, false);
				break;
			case Vars.TOGGLE:
				boolean gpsEnabled = Settings.Secure.isLocationProviderEnabled(cr, LocationManager.GPS_PROVIDER);
				if (gpsEnabled)
					Settings.Secure.setLocationProviderEnabled(cr, LocationManager.GPS_PROVIDER, false);
				else
					Settings.Secure.setLocationProviderEnabled(cr, LocationManager.GPS_PROVIDER, true);
				break;
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
			Toast.makeText(context, "toggle GPS need root", Toast.LENGTH_SHORT).show();
		}
	}
	
	//AirplaneMode
	public static void toggleAirplaneMode(byte opt, Context context)
	{
		try
		{
			ContentResolver cr = context.getContentResolver();
			Intent intent;
			switch (opt)
			{
			case Vars.ENABLE:
				Settings.System.putString(cr, Settings.System.AIRPLANE_MODE_ON, "1");
				intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
				context.sendBroadcast(intent);
				break;
			case Vars.DISABLE:
				Settings.System.putString(cr, Settings.System.AIRPLANE_MODE_ON, "0");
				intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
				context.sendBroadcast(intent);
				break;
			case Vars.TOGGLE:
				if (Settings.System.getString(cr, Settings.System.AIRPLANE_MODE_ON).equals("0"))
				{
					//获取当前飞行模式状�?返回的是String�?,�?.0为关闭飞�?1为开启飞�?
					//如果关闭飞行,则打开飞行
					Settings.System.putString(cr, Settings.System.AIRPLANE_MODE_ON, "1");
					intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
					context.sendBroadcast(intent);
				}
				else
				{
					//否则关闭飞行
					Settings.System.putString(cr, Settings.System.AIRPLANE_MODE_ON, "0");
					intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
					context.sendBroadcast(intent);
				}
				break;
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	
	//gprs
	public static void toggleMobileData(byte opt, Context context)
	{
		switch (opt)
		{
		case Vars.ENABLE:
			toggleMobileData(true, context);
			break;
		case Vars.DISABLE:
			toggleMobileData(false, context);
			break;
		case Vars.TOGGLE:
			ConnectivityManager cm;
			cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
			boolean isGprsConnected = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED ? true : false;
			if (isGprsConnected)
			{
				toggleMobileData(false, context);
			}
			else
			{
				toggleMobileData(true, context);
			}
			break;
		}
	}
	
	private static void toggleMobileData(boolean enabled, Context context)
	{
		ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		Method setMobileDataEnabl;
		try
		{
			setMobileDataEnabl = cm.getClass().getDeclaredMethod("setMobileDataEnabled", boolean.class);
			setMobileDataEnabl.setAccessible(true);
			setMobileDataEnabl.invoke(cm, enabled);
		}
		catch (Exception e)
		{
			Toast.makeText(context, "toggle GPRS need root", Toast.LENGTH_SHORT).show();
			e.printStackTrace();
		}
	}
	
	//RingerMode:silent & vibrate
	public static void toggleSilent(byte opt, Context context)
	{
		AudioManager mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
		int mode = mAudioManager.getRingerMode();
		boolean bv = mAudioManager.shouldVibrate(AudioManager.VIBRATE_TYPE_RINGER);
		boolean bs = (mode != AudioManager.RINGER_MODE_NORMAL);
		
		switch (opt)
		{
		case Vars.ENABLE:
			mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
			setRingerMode(true, bv, context);
			break;
		case Vars.DISABLE:
			setRingerMode(false, bv, context);
			break;
		case Vars.TOGGLE:
			setRingerMode(!bs, bv, context);
			break;
		}
	}
	
	public static void toggleVibrate(byte opt, Context context)
	{
		AudioManager mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
		int mode = mAudioManager.getRingerMode();
		boolean bv = mAudioManager.shouldVibrate(AudioManager.VIBRATE_TYPE_RINGER);
		boolean bs = (mode != AudioManager.RINGER_MODE_NORMAL);
		switch (opt)
		{
		case Vars.ENABLE:
			setRingerMode(bs, true, context);
			break;
		case Vars.DISABLE:
			setRingerMode(bs, false, context);
			break;
		case Vars.TOGGLE:
			setRingerMode(bs, !bv, context);
			break;
		}
	}
	
	private static void setRingerMode(boolean silent, boolean vibrate, Context context)
	{
		AudioManager mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
		if (silent)
		{
			mAudioManager.setRingerMode(vibrate ? AudioManager.RINGER_MODE_VIBRATE : AudioManager.RINGER_MODE_SILENT);
			mAudioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER, vibrate ? AudioManager.VIBRATE_SETTING_ON : AudioManager.VIBRATE_SETTING_OFF);
		}
		else
		{
			mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
			mAudioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER, vibrate ? AudioManager.VIBRATE_SETTING_ON : AudioManager.VIBRATE_SETTING_OFF);
		}
	}
	
	public static void setStreamVolume(Context context)
	{
		final AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View layout = inflater.inflate(R.layout.vol_seekbar, (ViewGroup) ((Activity) context).findViewById(R.id.vol_dialog));
		final SeekBar sb = (SeekBar) layout.findViewById(R.id.seekBar1);
		final TextView tv = (TextView) layout.findViewById(R.id.textView1);
		AlertDialog.Builder build = new AlertDialog.Builder(context);
		build.setIconAttribute(android.R.attr.alertDialogIcon);
		build.setTitle(R.string.hello_world);
		build.setView(layout);
		build.setPositiveButton(R.string.root, new DialogInterface.OnClickListener()
		{
			public void onClick(DialogInterface dialog, int whichButton)
			{
				int curr = sb.getProgress() + 1;
				am.setStreamVolume(AudioManager.STREAM_VOICE_CALL, curr, AudioManager.FLAG_VIBRATE | AudioManager.FLAG_ALLOW_RINGER_MODES | AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI);
			}
		});
		build.setNegativeButton(R.string.menu_settings, new DialogInterface.OnClickListener()
		{
			public void onClick(DialogInterface dialog, int whichButton)
			{
			}
		});
		AlertDialog alertDialog = build.create();
		alertDialog.show();
		
		int max = am.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL) - 1;
		int curr = 0;
		curr = am.getStreamVolume(AudioManager.STREAM_VOICE_CALL) - 1;
		sb.setMax(max);
		sb.setProgress(curr);
		tv.setText("" + (curr + 1));
		sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
		{
			public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
			{
				//Do something here with new value
				tv.setText("" + (progress + 1));
			}
			
			@Override
			public void onStartTrackingTouch(SeekBar seekBar)
			{
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onStopTrackingTouch(SeekBar seekBar)
			{
				// TODO Auto-generated method stub
				
			}
		});
	}
	
	public static void test(Context context)
	{
		//		byte opt = (byte)(Vars.TOGGLE);
		//		toggleWifi(opt, context);
		//		toggleBT(opt);
		//		toggleGPS(opt, context);
		//		toggleAirplaneMode(opt, context);
		//		toggleMobileData(opt, context);
		//		toggleVibrate(opt, context);
		//		toggleSilent(opt, context);
		//		setStreamVolume(context);
	}
}
